/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var pdCatimg = false;
var pdImages = ['', '', '', '', ''];
var pdId = false;

requirejs(['jquery', 'util', 'fancyBox', 'datatables', 'ztree', 'ztree_loader', 'ueditor', 'zxxFile', 'jUploader'], function($, util, fancyBox, dataTables, ztree, treeLoader, ueditor, jUploader) {
    $(function() {

        pdCatimg = $('#pd-catimg').val() === '' ? false : $('#pd-catimg').val();

        if ($('#mod').val() === 'edit') {
            // 编辑模式 存入图片列表
            $('.pd-images').each(function(i, node) {
                if ($(node).val() && $(node).val() !== '' && i < 5) {
                    pdImages[i] = $(node).val();
                    $('.pd-image-sec').eq(i + 1).addClass('ove').append('<img src="' + shoproot + 'static/product_hpic/' + $(node).val() + '" /></a>');
                }
            });
            pdId = parseInt($('#pid').val());
        } else {
            pdId = false;
        }

        // $("#pd-serial").find("option[value='" + $.cookie('serial') + "']")[0].selected = true;

        if (!pdId) {
            $('#pd-serial').on('change', function() {
                //$.cookie('serial', $(this).val());
            });
        }

        $('.pd-image-sec').each(function() {
            var btn = this;
            $(this).height($(this).width());
            $.jUploader({
                button: $(btn),
                action: shoproot + '?/vProduct/ImageUpload',
                onComplete: function(fileName, response) {
                    var Btn = $(this.button[0]);
                    var R = response;
                    if (R.s > 0) {
                        var iid = parseInt(Btn.attr('data-id'));
                        var src = shoproot + 'static/product_hpic2/' + R.imgn;
                        Btn.addClass('ove');
                        util.Alert('图片上传成功');
                        if (Btn.find('img').length > 0) {
                            Btn.find('img').attr('src', src);
                        } else {
                            Btn.append('<img src="' + src + '" /></a>');
                        }
                        if (!pdCatimg || iid === 0) {
                            pdCatimg = 'product_hpic2__' + R.imgn;
                        }
                        if (iid !== 0) {
                            pdImages[iid - 1] = 'product_hpic2__' + R.imgn;
                        }
                    } else {
                        util.Alert('上传图片失败，请联系技术支持');
                    }
                }
            });
            $(this).hover(function() {
                if (!$(this).hasClass('ove')) {
                    $(this).addClass('hover');
                }
            }, function() {
                if (!$(this).hasClass('ove')) {
                    $(this).removeClass('hover');
                }
            });
        });

        if (pdCatimg) {
            $('.pd-image-sec').eq(0).addClass('ove').append('<img src="' + shoproot + 'static/product_hpic/' + pdCatimg + '" /></a>');
        }

        $('body').css('overflow-x', 'hidden');

        uep = UM.getEditor('ueditorp', {
            autoHeight: true
        });
        uep.ready(function() {
            uep.setWidth('100%');
            ueploaded = true;
        });

        // 图片已经上传过了。
        $('#save_product_btn').unbind('click').click(__ProductAlterFinish);
    });

    /**
     * 商品编辑结束
     * @returns {undefined}
     */
    function __ProductAlterFinish() {
        var title = $('#pd-form-title').val();
        var price = parseFloat($('#pd-form-prices').val());
        var discount = $('#pd-form-discount').val();
        var desc = uep.getContent();
        var subtitle = $('#pd-form-desc').val();
        // [HttpPost]
        $.post(shoproot + '?/WdminAjax/updateProduct', {
            product_id: !pdId ? 0 : pdId,
            product_name: title,
            product_prices: price > 0 ? price : 0,
            product_discount: discount,
            product_desc: desc,
            product_subtitle: subtitle,
            product_images: pdImages,
            product_cat: $('#pd-form-cat').val(),
            product_serial: $("#pd-serial").val(),
            catimg: pdCatimg ? pdCatimg : ''
        }, function(r) {
            if (r === '1') {
                if (!pdId) {
                    util.Alert('添加成功', false, function() {
                        history.go(-1);
                    });
                } else {
                    util.Alert('保存成功');
                }
            } else {
                util.Alert('保存失败');
            }
        });
    }
});